package gl.android.biyiqing.competent;

import gl.adapter.SimpleAdapter;
import gl.android.biyiqing.BYQBaseActivity;
import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.Result;
import gl.android.biyiqing.event.EventCalnceFollow;
import gl.android.biyiqing.event.EventOnFollow;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.Tip;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huewu.pla.lib.internal.PLA_AdapterView;

import de.greenrobot.event.EventBus;

public class OnToFollowUserClickListener implements OnItemClickListener,
		com.huewu.pla.lib.internal.PLA_AdapterView.OnItemClickListener,
		SimpleAdapter.OnItemChildViewClick ,View.OnClickListener{

	private String jsonArrayData;
	private String userID;
	private boolean isfriend = false;

	public OnToFollowUserClickListener(Context mContext, String jsonArrayData,
			String[] from) {
		this.jsonArrayData = jsonArrayData;
	}
	public OnToFollowUserClickListener(Context mContext, String userID,boolean isfriend
			) {
		this.userID = userID;
		this.isfriend = isfriend;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		click(arg2);
	}

	private void click(int position) {
		Type stringStringMap = new TypeToken<ArrayList<Map<String, String>>>() {
		}.getType();
		List<Map<String, String>> map = new Gson().fromJson(jsonArrayData,
				stringStringMap);
		String userid = map.get(position).get(BYQBaseActivity.KEY_USER_ID);
		try {
			isfriend = Integer.parseInt(map.get(position).get("isFriend"))==1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		folow(userid);

	}
	
	public void folow(String userid) {
		if (isfriend) {
//			0 no 1 yes
			BaseControl.get(API.URL + "CancelFriend?userid=%s&fuserid=%s",
					new ByqAjaxCallProxy() {
						@Override
						public void onSuccess(String t) {
							super.onSuccess(t);
							try {
								Result result = unformat(t);
								if ("0".equals(result.getMsg())) {
									Tip.show("取消关注失败");
								} else if ("1".equals(result.getMsg())) {
									Tip.show("取消关注失败 ");
								} else if ("2".equals(result.getMsg())) {
									Tip.show("取消关注成功");
									EventBus.getDefault().post(new EventCalnceFollow());
								}
							} catch (Exception e) {
								e.printStackTrace();
							}

						}
					}, new Object[] { userid, UserManger.getInstance().userid });
		}else{
			BaseControl.get(API.URL + "TakeFriend?userid=%s&fuserid=%s",
					new ByqAjaxCallProxy() {
						@Override
						public void onSuccess(String t) {
							super.onSuccess(t);
							try {
								Result result = unformat(t);
								if ("0".equals(result.getMsg())) {
									Tip.show("已经关注");
								} else if ("1".equals(result.getMsg())) {
									Tip.show("关注失败  ");
								} else if ("2".equals(result.getMsg())) {
									Tip.show("关注成功！");
									EventBus.getDefault().post(new EventOnFollow());
								}
							} catch (Exception e) {
								e.printStackTrace();
							}

						}
					}, new Object[] { userid, UserManger.getInstance().userid });
		}
		
	}

	@Override
	public void onItemClick(PLA_AdapterView<?> parent, View view, int position,
			long id) {
		click(position);
	}

	@Override
	public void onClick(View view, int position) {
		click(position);
	}

	@Override
	public void onClick(View v) {
		folow(userID);
		final Animation loadAnimation = AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);
		loadAnimation.setDuration(500);
		v.startAnimation(loadAnimation);
	}

}
